﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcDeviceManager.Models;
using System.Web.Security;
using MvcDeviceManager.Service;
using MvcDeviceManager.Filter;

namespace MvcDeviceManager.Controllers
{
    public class LoginController : Controller
    {
        MenuService MenuService = new MenuService();
        RoleAuthorityService roleAuthorityService = new RoleAuthorityService();
        MenuActionService menuActionService = new MenuActionService();
        SystemParameterService systemParameterService = new SystemParameterService();
        //
        // GET: /User/

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogIn logIn, string ReturnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(logIn.UserName, logIn.Password))
                {
                    //FormsAuthentication.SetAuthCookie(logIn.UserName, true);
                    Session[Constant.LOGIN_USER] = logIn.UserName;
                    initUserData();
                    if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/")
                        && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))
                    {
                        return Redirect(ReturnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "对不起，用户名或密码错误，请重新输入！");
                }
            }
            return View(logIn);
        }

        //
        // GET: /User/

        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("LogOn", "Login");
        }

        public ActionResult NoAuthority()
        {
            return View();
        }

        private void initUserData()
        {
            //Session[Constant.LOGIN_USER].ToString()
            //HttpContext.User.Identity.Name
            string[] roles = Roles.GetRolesForUser(Session[Constant.LOGIN_USER].ToString());
            IList<Menu> menus;
            //if (roles.Contains("admin"))
            //{
            //    menus = MenuService.getMenuTree();
            //}
            //else
            //{
                IList<RoleAuthority> ras = roleAuthorityService.listUsersRoleAllAuthority(roles);
                IList<MenuAction> actions = menuActionService.listMenuActionByRoleAuthority(ras);
                
                ISet<string> menuIds = new HashSet<String>();
                foreach (MenuAction ma in actions)
                {
                    if (ma.MenuId != null)
                    {
                        menuIds.Add(ma.MenuId);
                    }
                }
                IList<SystemParameter> commonsMenu = systemParameterService.GetAllCommonsMenu();
                foreach (SystemParameter sp in commonsMenu)
                {
                    menuIds.Add(sp.Value);
                }
                menus = MenuService.getMenuTreeByMenuIds(menuIds);
            //}
           
            
            Session[Constant.MENU] = menus;
        }
    }
}
